package main

import (
	"encoding/json"
	"fmt"
	"io"
	"math"
	"net/http"
	"os"
)

type Shape interface {
	Perimeter() float64
	Area() float64
}

type Square struct {
	size float64
}

func (s Square) Area() float64 {
	return s.size * s.size
}

func (s Square) Perimeter() float64 {
	return s.size * 4
}

type Circle struct {
	radius float64
}

func (c Circle) Area() float64 {
	return math.Pi * c.radius * c.radius
}

func (c Circle) Perimeter() float64 {
	return 2 * math.Pi * c.radius
}

type Stringer interface {
	String() string
}

type Person struct {
	Name, Country string
}

func (p Person) String() string {
	return fmt.Sprintf("%v is from %v", p.Name, p.Country)
}

type customWriter struct{}

type GithubResponse []struct {
	FullName string `json:"full_name"`
}

func (w customWriter) Write(p []byte) (n int, err error) {
	var resp GithubResponse
	json.Unmarshal(p, &resp)
	for _, r := range resp {
		fmt.Println(r.FullName)
	}
	return len(p), nil
}

func githubTest() {
	resp, err := http.Get("https://api.github.com/users/microsoft/repos?page=15&per_page=5")
	if err != nil {
		fmt.Println("Error", err)
		os.Exit(1)
	}
	writer := customWriter{}
	io.Copy(writer, resp.Body)
}

func printInfomation(s Shape) {
	fmt.Println("%T/n", s)
	fmt.Println("Area:", s.Area())
	fmt.Println("Perimeter:", s.Perimeter())
}

func microsoft() {
	resp, err := http.Get("https://api.github.com/users/microsoft/repos?page=15&per_page=5")
	if err != nil {
		fmt.Println("Error", err)
		os.Exit(1)
	}
	io.Copy(os.Stdout, resp.Body)

}

func main() {
	// fmt.Printf("%s\n", "hello world")
	// var s Shape = Square{3}
	// printInfomation(s)
	// var c Shape = Circle{3}
	// printInfomation(c)

	// var p Stringer = Person{"wangheng", "jinan"}
	// fmt.Println(p.String())
	// var t Stringer = Person{"test", "qingdao"}
	// fmt.Println(t.String())

	// microsoft()
	githubTest()
}
